﻿$(function() {
	var expressList = $('#expressList').datagrid({
	    title:'快递网点信息', 
	    fitColumns:true,
	    url:'express!getExpressPage?returnType=json',
	    idField:'expressId', 
	    singleSelect:false,//是否单选 
	    pagination:true,//分页控件 
	    rownumbers:true,//行号 
	    toolbar: [{
	        text: '添加', 
	        iconCls: 'icon-add', 
	        handler: function() { // 回调函数
	            var expressAddDialog = $('#express_addDialog').dialog({
	                width: 750,
	                height: 350,
	                href: 'jsp/express/expressAdd.jsp',
	                modal: true,
	                shadow: false,
	                title: '添加信息',
	                onLoad:function(){
	                	initProvCity("#express_addForm","福建","福州");
	                	initCompany();
	                },
	                buttons: [{
	                    text: '确定',
	                    iconCls: 'icon-add',
	                    handler: function() {
	                    		$('#express_addForm').form('submit', {
	                    			url: 'saveOrUpateIpaddress.do?type=1',
	                    			success: function(j) {
	                    				var result = jQuery.parseJSON(j);
	                    				if (result.success) {
	                    					$("#expressList").datagrid('reload');
	                    					$("#expressList").datagrid('unselectAll');
	                    					expressAddDialog.dialog('close');
	                    				}
	                    				showMsg(result.msg);
	                    			}
	                    		});
	                    }
	                },
	                {
	                    text: '取消',
	                    iconCls: 'icon-remove',
	                    handler: function() {
	                        $('#express_addDialog').dialog('close');
	                    }
	                }]
	            });
	            $('#express_addDialog').window('center');
	        } 
	    }, '-', { 
	        text: '修改', 
	        iconCls: 'icon-edit', 
	        handler: function() { 
	            openDialog("express_updateDialog","edit"); 
	        } 
	    }, '-',{ 
	        text: '删除', 
	        iconCls: 'icon-remove', 
	        handler: function(){
                var rows = expressList.datagrid('getSelections');
                if (rows.length <= 0) {
                    $.messager.alert('提示', '请选择要删除的记录!', 'info');
                } else {
                    var ids = "";
                    for (var i = 0; i < rows.length; i++) {
                    	if(i==(rows.length-1)){
                    		ids = ids + rows[i].expressId;
                    	}else{
                    		ids = ids + rows[i].expressId + ',';
                    	}
                    }
                    $.messager.confirm('提示', '您确定要删除选择的记录吗?',
                    function(t) {
                        if (t) {
                            $.ajax({
                                type: 'post',
                                url: 'express!deleteExpress?ids=' + ids,
                                success: function(result) {
                                    if (result.success) {
                                        expressList.datagrid('acceptChanges');
                                        editRow = undefined;
                                        expressList.datagrid('reload');
                                    }
                                    expressList.datagrid('unselectAll');
                                    showMsg(result.msg);
                                }
                            });

                        }
                    });
                }
            } 
	    }], 
	});
	//右下角提示
	function showMsg(message){
		$.messager.show({
			title: '提示',
			msg: message
		});
	}
	//初始化省市信息
	function initProvCity(dom,p,c){
		$(dom).citySelect({
			nodata:"none",
			required:true,
			prov:p,
			city:c
		}); 
	}
	//初始化快递公司下拉表格
	function initCompany(){
		$('#company').combogrid({  
			delay:1000,//1s延迟查询，为输入中文考虑
            panelWidth:500,
            idField:'companyId', //ID字段  
            textField:'companyName', //显示的字段  
            url:"company!getCompanyPage?returnType=json",  
            fitColumns: true,  
            striped: true,  
            editable:true,  
            pagination : true,//是否分页  
            rownumbers:true,//序号  
            collapsible:false,//是否可折叠的  
            fit: true,//自动大小  
            pageSize: 10,//每页显示的记录条数，默认为10  
            pageList: [10],//可以设置每页记录条数的列表  
            method:'post',  
            columns:[[
                {field:'companyName',title:'公司名称',width:100,sortable:true}
            ]],
            keyHandler: {
                up: function() {},
                down: function() {},
                enter: function() {},
                query: function(q) {
                    //动态搜索
                    $('#company').combogrid("grid").datagrid("reload", { 'keyword': q });
                    $('#company').combogrid("setValue", q);
                }
            }
        }); 
	}
});